package com.sky.sd.system.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sky.sd.common.web.response.Result;
import com.sky.sd.system.bean.dto.MenuNode;
import com.sky.sd.system.bean.po.Menu;
import com.sky.sd.system.bean.po.Role;
import com.sky.sd.system.bean.vo.*;
import com.sky.sd.system.enums.MenuType;
import com.sky.sd.system.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统菜单 前端控制器
 * </p>
 *
 * @author 弓振
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/system/menu")
@Api(tags = "系统菜单")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @GetMapping("/me")
    @ApiOperation(value = "查询当前登录用户权限菜单")
    public Result MenuMe() {
        List<MenuNode> menuNodes = menuService.listLoginUserOfMenu();
        return Result.success(menuNodes);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询所有菜单列表")
    public Result listMenu() {
        List<MenuNode> menuNodes = menuService.lisMenu();
        return Result.success(menuNodes);
    }

    @GetMapping("/check/code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "菜单编码", dataType = "string", paramType = "query", example = "",required = true)
    })
    @ApiOperation(value = "菜单编码是否存在")
    public Result check(MenuCheckVo vo) {
        int cnt = menuService.count(Wrappers.<Menu>lambdaQuery().ne(StringUtils.isNotBlank(vo.getId()), Menu::getId, vo.getId())
                .eq(Menu::getCode, vo.getCode()).eq(Menu::getType, MenuType.MENU));
        return Result.success(cnt);
    }

    @GetMapping("/check/parentId")
    @ApiOperation(value = "验证当前选择的父菜单是否是子节点或当前节点")
    public Result checkParentId(String parentId,String id) {
        int cnt = menuService.checkParentId(parentId, id);
        return Result.success(cnt);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增菜单")
    public Result addMenu(@RequestBody MenuAddVo vo) {
        menuService.add(vo);
        return Result.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改菜单")
    public Result updateMenu(@RequestBody MenuUpdateVo vo) {
        menuService.update(vo);
        return Result.success();
    }

    @PostMapping("/update/enabled")
    @ApiOperation(value = "修改菜单状态")
    public Result updateMenuEnabled(@RequestBody MenuUpdateVo vo) {
        menuService.updateEnabled(vo);
        return Result.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除菜单")
    public Result deleteMenu(@RequestBody DeleteVo vo) {
        menuService.delete(vo);
        return Result.success();
    }
}